define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {


        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/order/index' + location.search,
                    add_url: 'chase/order/add',
                    edit_url: 'chase/order/edit',
                    del_url: 'chase/order/del',
                    multi_url: 'chase/order/multi',
                    table: 'orders',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'username', title: __('到店消费人')},
                        {field: 'design_id', title: __('Design_id')},
                        {field: 'design_service_id', title: __('Design_service_id')},
                        {field: 'user_coupon_id', title: __('User_coupon_id')},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'time', title: __('Time')},
                        {field: 'service_name', title: __('Service_name')},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: __('Discount_amount'), operate:'BETWEEN'},
                        {field: 'vip_amount', title: __('Vip_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: __('Type'), searchList: {"wechat":__('Type wechat'),"account":__('Type account')}, formatter: Table.api.formatter.normal},
                        {field: 'is_comment', title: __('Is_comment'), searchList: {"0":__('Is_comment 0'),"1":__('Is_comment 1')}, formatter: Table.api.formatter.normal},
                        {field: 'comment_time', title: __('Comment_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('修改'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-edit',
                                    url: 'chase/order/edit',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 0) {
                                            return true;
                                        }
                                    }
                                },
                                // {
                                //     name: 'detail',
                                //     title: __('付款'),
                                //     classname: 'btn btn-xs btn-info btn-dialog',
                                //     icon: 'fa fa-camera-retro',
                                //     url: 'chase/order/detail',
                                //     callback: function (data) {
                                //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //     },
                                //     visible: function (row) {
                                //         //返回true时按钮显示,返回false隐藏
                                //         if (row.status == 0) {
                                //             return true;
                                //         }
                                //     }
                                // }
                                ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        user: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'chase/order/user' + location.search,
                    add_url: 'chase/order/add',
                    edit_url: 'chase/order/edit',
                    del_url: 'chase/order/del',
                    multi_url: 'chase/order/multi',
                    table: 'orders',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'design_id', title: __('Design_id')},
                        {field: 'design_service_id', title: __('Design_service_id')},
                        {field: 'user_coupon_id', title: __('User_coupon_id')},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'time', title: __('Time')},
                        {field: 'service_name', title: __('Service_name')},
                        {field: 'total_amount', title: __('Total_amount'), operate:'BETWEEN'},
                        {field: 'discount_amount', title: __('Discount_amount'), operate:'BETWEEN'},
                        {field: 'vip_amount', title: __('Vip_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'type', title: __('Type'), searchList: {"wechat":__('Type wechat'),"account":__('Type account')}, formatter: Table.api.formatter.normal},
                        {field: 'is_comment', title: __('Is_comment'), searchList: {"0":__('Is_comment 0'),"1":__('Is_comment 1')}, formatter: Table.api.formatter.normal},
                        {field: 'comment_time', title: __('Comment_time'), operate: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'orderdata', title: __('订单类型'), operate: false, searchList: {"1":__('预约'),"2":__('买单')}, formatter: Table.api.formatter.label},
                        {field: 'createtime', title: __('Createtime'), operate: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'chase/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'chase/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'chase/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            //服务价格
            $('#c-design_service_id').on('change', function () {
                var design_service_id = $('#c-design_service_id').val()
                $.get('chase/order/price', {"design_service_id": design_service_id}, function (res) {
                    //服务价格
                    $('#c-price').val(res.price)
                    var service_pirce =  $('#c-price').val()
                    //服务名称
                    $('#c-service_name').val(res.service.service_name)
                    //会员优惠
                    var user_discount = $('#c-user_discount').val()
                    if (user_discount > 0) {
                        $('#c-vip_amount').val(service_pirce * (100 - user_discount) / 100)
                    }
                    var vip_amount = $('#c-vip_amount').val()
                    //订单总价
                    $('#c-total_amount').val(service_pirce - vip_amount)
                })
            })

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});